<template>
  <div class="app-container">
    <!-- 字典项配置抽屉 -->
    <el-drawer
      title="分配用户角色"
      :visible.sync="visible"
      :direction="direction"
      size="25%"
    >
      <div class="app-container">
        <el-form
          ref="ruleForm"
          label-width="80px"
          class="demo-ruleForm"
          label-position="top"
        >
          <el-form-item label="用户角色:">
            <el-select
              v-model="selectedIds"
              multiple
              placeholder="请选择用户角色"
              style="width: 100%"
            >
              <el-option
                v-for="item in selectedRoles"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-plus"
              @click.native.prevent="updateUserRole"
            >
              更新角色
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import role from '@/api/system/role'
import userRole from '@/api/system/userRole'
export default {
  name: 'UserDrawer',
  filters: {},
  props: {
    roles: {
      type: Array
    }
  },
  data() {
    return {
      loading: true,
      selectedIds: [],
      selectedRoles: [],
      hadRole: [],
      visible: false,
      direction: 'rtl',
      userId: ''
    }
  },
  watch: {
    roles() {
      this.roles.forEach((role) => {
        this.selectedRoles.push({ value: role.id, label: role.roleName })
      })
    }
  },
  created() {
    // 备份默认值
  },
  methods: {
    updateUserRole() {
      userRole
        .updateUserRole({
          userId: this.userId,
          roleIds: this.selectedIds.join(',')
        })
        .then(
          (res) => {
            this.$message({
              type: 'success',
              message: res.message
            })
            // removeToken();
            // this.$router.push({ path: "/login" });
            // console.log(this.$router);
            this.loading = false
            this.visible = false
          },
          (err) => {
            this.$message({
              type: 'error',
              message: err.message
            })
            this.loading = false
            this.visible = false
          }
        )
    },
    getUserRoleByUserId() {
      role.getUserRoleByUserId({ userId: this.userId }).then((res) => {
        console.log(res)
        this.hadRole = res.result
        this.hadRole.forEach((role) => {
          this.selectedIds.push(role.id)
        })
      })
    },
    //
    defaultMethod() {},
    // 获取分页数据
    getTableData(arg) {},
    // 分页改变
    handleCurrentChange(currentPage) {
      this.queryParam.pageNo = currentPage
      this.getTableData()
    },
    // 添加
    handleAdd() {
      console.log('handleAdd')
    },
    // 删除
    handleDelete(id) {
      console.log('handleDelete')
      dict.deleteById({ id: id }).then(
        (res) => {
          console.log(res)
          this.$message({
            type: 'success',
            message: res.message
          })
          this.loading = false
          this.handleOK()
        },
        (err) => {
          console.log(err)
          this.$message({
            type: 'error',
            message: err.message
          })
          this.loading = false
          this.getTableData()
        }
      )
    },
    // 编辑
    handleEdit(index, row) {
      console.log('handleEdit')
    },
    // 搜索
    searchQuery() {
      this.getTableData(1)
    },
    // 重置
    reset() {
      this.getTableData()
    },
    // 增删改刷新数据
    handleOK() {
      this.selectedIds = []
      this.selectedDataAll = []
      this.getTableData()
    }
  }
}
</script>
